package com.bumpslide.ui.skin 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	[DefaultProperty('children')]

	/**
	 * This is the base class for programmatic stateful skins
	 * 
	 * The ISkin draw method is implented in such a way that it looks
	 * for a method on the skin class with a name like '_'+hostComponent.skinState
	 * 
	 * If found, this method is called.  An example of this can be found in the 
	 * minimal button skin class.
	 * 
	 * @author David Knape
	 */
	public class BasicSkin extends Sprite implements ISkin 
	{			
	
		/**
		 * reference to component that is hosting this skin
		 */
		protected var _hostComponent:ISkinnable;

		
		/**
		 * Default state change handler
		 * 
		 * Tries to call a function in this class with a name that is ('_' + currentState).
		 */
		public function draw( skinState:String ):void
		{
			try {
				(this['_'+_hostComponent.skinState] as Function).call( this);
			} catch (e:Error) {
				// whatever
			}
		}
		
		
		public function init(host_component:ISkinnable):void
		{
			_hostComponent = host_component;
		}
		
		
		public function destroy():void
		{
			
		}
		
		/**
		 * MXML DefaultProperty, receieves references to classes added as child nodes 
		 * after this component has been initialized
		 */
		public function set children(value:Array):void {
			for each(var o:* in value) {
				var child:DisplayObject = (o as DisplayObject);
				if(child != null) {
					// add to the display list
					addChild(child as DisplayObject);					
					// give reference to parent class 
					// so we can use for skinning 
					try { 
						this[child['id']] = child; 
					} catch (e:Error) {
					} // fail silently
				} 
			}	
			trace('added children to ' + this );
			//dispatchEvent(new Event(EVENT_CREATION_COMPLETE));
		}
		
		
		
//		/**
//		 * Example State render function: Draw skin state "selected"
//		 */
//		 private function _selected():void {
//		 	
//		 }
		
	}
}
